package com.database.dao.impl;

import java.util.List;

import javax.inject.Named;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.database.bean.Agent;
import com.database.dao.AgentDao;

@Named("agentDao")
@Component
public class AgentDaoImpl extends BaseDaoImpl<Agent> implements AgentDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		logger.debug("Set Autowired SessionFactory: {}",sessionFactory);
		this.sessionFactory = sessionFactory;
	}
	
	public AgentDaoImpl() {
		super(Agent.class);
	}


	public void deleteAgentWithAgentTypeId(Integer agentTypeId) {
		Session session = sessionFactory.openSession();
		try {
			String sql = "delete from agent where agenttype_id="+agentTypeId;
			SQLQuery query = session.createSQLQuery(sql);
			
			query.executeUpdate();

		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	
	/**
	 * Method to get all Agents to a specific AgentType
	 * @param game
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Agent> getAgentsWithAgentTypeId(Integer agentTypeId) {
		List<Agent> result=null;
		Session session = sessionFactory.openSession();
		try {
			String sql="select * from Agent where agenttype_id="+agentTypeId;
			SQLQuery query = session.createSQLQuery(sql);
			query.addEntity(Agent.class);
			result= query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		return result;
	}
	
}
